const api = require("../../../../utils/cloudAPIUtils");
const app = getApp();

Page({
  data: {
      setep:1,
      progressBar:0,
      slot:0,
      msg:''
  },

  toIndex(){
      wx.reLaunch({
        url: '/pages/index/index',
      })
  },

  onLoad(options) {
    // 1秒钟走2%进度，100%耗时50s
    // 2秒调用一次
    // 结果值：1等待 2成功 3关闭
    const self = this;
    let num = 0;
    let timer = setInterval(()=>{
        num++;
        if(num>=92){
            clearInterval(timer);
            self.setData({
                setep:3,
                msg:'请求超时，请稍后再试！'
            })
        }
        self.setData({progressBar:num});
        if(num%4==0){
            api.post(api.url.cdbResult,{
                tradeNo:options.tradeNo,
                payType:3
            },res=>{
                if(res.data.body.resultCode==2){
                    clearInterval(timer);
                    self.setData({
                        progressBar:100,
                    });
                    setTimeout(()=>{
                        self.setData({
                            setep:2,
                            slot:res.data.body.slotNum||0,
                        })
                    },600)
                }else if(res.data.body.resultCode==3){
                    clearInterval(timer);
                    self.setData({
                        setep:3,
                        progressBar:100,
                    });
                }
            })
        }
    },500)
  },
})